<?php

namespace App\Http\Controllers\Admin\SharePrize;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class PrizeConfigController extends BaseController
{

    /**
     * Note: 创建配置
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/prizeconfig/create
     */
    public function create(Request $request)
    {
        $param = $request->all();
        unset($param['s']);

        $v = app('validator')->make($param, [
            'period_id' => 'required',
            'sort' => 'required',
            'prize_type' => 'required',
            'cate_id' => 'required',
            'product_id' => 'required',
            'product_name' => 'required',
            'number' => 'required',
            'prize_choose_type' => 'required', //'1=普通,2=稀有,3=史诗,4=传说'
            'cost' => 'required',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $param['month'] = $param['period_id'];
        unset($param['period_id']);

        $periodInfo = app('model_shareprize_period')->where(['id' => $param['month']])->first();
        $param['start_time'] = $periodInfo->start_time;
        $param['end_time'] = $periodInfo->end_time;
        $res = app('model_shareprize_config')->insertGetId($param);

        return Response::sendData($res);
    }

    /**
     * Note: 获取配置列表
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/prizeconfig/list
     */
    public function list(Request $request)
    {
        $params = $request->all();
        $v = app('validator')->make($params, [
            'period_id' => 'required',
            'page' => 'required|integer|min:1',
            'page_size' => 'required|integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $where = ['deleted_at' => null, 'month' => $params['period_id']];
        //1=商品类,2=金币类
        if (!empty($params['prize_type'])) {
            $where['prize_type'] = $params['prize_type'];
        }
        //商品分类id
        if (!empty($params['cate_id'])) {
            $where['cate_id'] = $params['cate_id'];
        }
        //1=普通,2=稀有,3=史诗,4=传说
        if (!empty($params['prize_choose_type'])) {
            $where['prize_choose_type'] = $params['prize_choose_type'];
        }
        $list = app('model_shareprize_config')
            ->where($where)
            ->orderBy('id', 'desc')
            ->paginate($params['page_size'], ['*'], '', $params['page']);

        foreach ($list as &$value) {
            switch ($value->prize_choose_type) {
                case '1':
                    $value->level = '普通';
                    break;
                case '2':
                    $value->level = '稀有';
                    break;
                case '3':
                    $value->level = '史诗';
                    break;
                case '4':
                    $value->level = '传说';
                    break;
            }
        }

        return Response::sendData($list);
    }

    /**
     * Note: 编辑
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/prizeconfig/edit
     */
    public function edit(Request $request)
    {
        $param = $request->all();

        $v = app('validator')->make($param, [
            'id' => 'required',
            'period_id' => 'required',
            'sort' => 'required',
            'prize_type' => 'required',
            'cate_id' => 'required',
            'product_id' => 'required',
            'product_name' => 'required',
            'number' => 'required',
            'prize_choose_type' => 'required', //'1=普通,2=稀有,3=史诗,4=传说'
            'cost' => 'required',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $id = $param['id'];
        unset($param['s']);
        unset($param['id']);
        $param['month'] = $param['period_id'];
        unset($param['period_id']);
        $res = app('model_shareprize_config')->where(['id' => $id])->update($param);


        return Response::sendData($res);
    }

    /**
     * Note: 删除配置
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {post} /shareprize/prizeconfig/delete
     */
    public function delete(Request $request)
    {
        $param = $request->all();
        $v = app('validator')->make($param, [
            'id' => 'required',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $id = $param['id'];
        $ids = explode(',', $id);

        $res = app('model_shareprize_config')->whereIn('id', $ids)->update(['deleted_at' => date("Y-m-d H:i:s")]);

        return Response::sendData($res);
    }

    /**
     * Note: 分类列表
     * author: wangzefei
     * Date: 2022-02-16 15:07:56
     * param {Type} $var
     * return {*}
     * /shareprize/prizeconfig/cate
     */
    public function cate()
    {
        $list = app('model_mall_categories')
            ->select('id', 'title', 'icon', 'is_show', 'sort')
            ->where('new_parent_id', 0)
            ->whereIn('id', [1, 2, 4, 23, 24, 25, 26, 27, 33])
            ->orderBy('new_sort', 'asc')
            ->get()->toArray();
        return Response::sendData($list);
    }

    /**
     * Note: 一键导入上期数据
     * author: wangzefei
     * Date: 2022-02-16 17:45:58
     * param {*}
     * return {*}
     * /shareprize/prizeconfig/importperiod
     */
    public function importPeriod(Request $request)
    {
        $param = $request->all();
        $v = app('validator')->make($param, [
            'period_id' => 'required',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $id = $param['period_id'];
        //获取上期的期数id
        $where['deleted_at'] = null;
        $where[] = ['id', "<", $id];
        $lastPeriod = app('model_shareprize_period')->where($where)->orderBy('id', 'desc')->value('id') ?? 0;
        if ($lastPeriod == 0) {
            throw new BasicException(10001, '没有上期数据');
        }
        $configWhere['deleted_at'] = null;
        $configWhere['is_show'] = 1;
        $configWhere['month'] = $lastPeriod;

        $periodInfo = app('model_shareprize_period')->where(['id' => $id])->first();
        //获取上期数据
        $userData = app('model_shareprize_config')->where($configWhere)->orderBy("prize_choose_type", 'asc')->get([
            "start_time",
            "end_time",
            "sort",
            "prize_type",
            "cate_id",
            "product_id",
            "product_name",
            "number",
            "prize_choose_type",
            "cost",
            "is_show",
        ])->toarray();

        foreach ($userData as &$value) {
            $value['month'] = $id;
            $value['start_time'] = $periodInfo->start_time;
            $value['end_time'] = $periodInfo->end_time;
        }
        $res =  app('model_shareprize_config')->addAll($userData);

        return Response::sendData($res);
    }
}
